package com.cxx.food.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.cxx.food.dto.ReadView;
import com.cxx.food.dto.UserDto;
import com.cxx.food.dto.WriteView;
import com.cxx.food.service.UserService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
public class UserController {
    @Resource
    private UserService userService;

    @Operation(summary = "注册用户")
    @PostMapping("")
    @SaIgnore
    public Boolean registerUser(@RequestParam("username") String username,
                                @RequestParam("password") String password) {
        return userService.register(username, password);
    }

    @Operation(summary = "查询用户")
    @GetMapping("/{id}")
    public @JsonView(ReadView.class) UserDto queryUser(@PathVariable("id") Long id) {
        return userService.queryUser(id);
    }

    @Operation(summary = "更新用户")
    @PutMapping("/{id}")
    public Boolean updateUser(@PathVariable("id") Long id,
                              @Validated @RequestBody UserDto userDto) {
        return userService.updateUser(id, userDto);
    }
}